package com.qk.controller;

import com.qk.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;
    //角色列表数据查询
    @GetMapping("/roles")
    public Result list(String name, String label, @RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5")Integer pageSize){

        PageResult<Role> pageResult = roleService.listRoles(name, label, page, pageSize);
        return Result.success(pageResult);
    }
    //删除角色
    @DeleteMapping("/roles/{id}")
    public Result delete(@PathVariable Integer id){
        roleService.deleteById(id);
        return Result.success();
    }

    //添加角色
    @PostMapping("/roles")
    public Result add(@RequestBody Role role){
        roleService.add(role);
        return Result.success();
    }

    //根据id查询
    @GetMapping("/roles/{id}")
    public Result getRoleById(@PathVariable Integer id){
       Role role = roleService.getRoleById(id);
       return Result.success(role);
    }
    //修改角色
    @PutMapping("/roles")
    public Result update(@RequestBody Role role){
        roleService.update(role);
        return Result.success();
    }

    //查询所有角色
    @GetMapping("/roles/list")
    public Result listRoles(){
     List<Role> roles = roleService.listAllRoles();
        return Result.success(roles);
    }



}
